package org.dromara.common.web.config

import jakarta.servlet.DispatcherType
import org.dromara.common.web.config.properties.XssProperties
import org.dromara.common.web.filter.RepeatableFilter
import org.dromara.common.web.filter.XssFilter
import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.web.servlet.FilterRegistration
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.context.annotation.Bean

/**
 * Filter配置
 *
 * @author LikeYouDo
 * @date 2025/07/26
 */
@AutoConfiguration
@EnableConfigurationProperties(XssProperties::class)
class FilterConfig {
    @Bean
    @ConditionalOnProperty(value = ["xss.enabled"], havingValue = "true")
    @FilterRegistration(
        name = "xssFilter",
        urlPatterns = ["/*"],
        order = FilterRegistrationBean.HIGHEST_PRECEDENCE + 1,
        dispatcherTypes = [DispatcherType.REQUEST]
    )
    fun xssFilter(): XssFilter {
        return XssFilter()
    }

    @Bean
    @FilterRegistration(name = "repeatableFilter", urlPatterns = ["/*"])
    fun repeatableFilter(): RepeatableFilter {
        return RepeatableFilter()
    }
}
